﻿using System;
using System.Collections.Generic;
using System.ComponentModel;
using System.Data;
using System.Drawing;
using System.Linq;
using System.Text;
using System.Windows.Forms;
using System.Data.SqlClient;
using System.Configuration;
using System.Windows.Forms;
using zytech.common.org.bookintake.service;
namespace zytech.common
{
    public partial class LoginForm : Form
    {
        public static string name, pwd, username;  //用户ID、密码、姓名
        Timer myTimer = new Timer();//实例化一个timer

        int j = 0;      //用户名密码输错的次数
        int cipid = 1000;
        BookintakeWebService serivce = new BookintakeWebService();
        string SqlConnStr = ConfigurationManager.ConnectionStrings["SqlConnectionString"].ConnectionString;
        public LoginForm()
        {
            InitializeComponent();
        }
        /// <summary>
        /// 设置登陆后打开哪个窗口，需在继承的窗体中重写
        /// </summary>
        protected virtual void setMain()
        {
            //Form2 mf = new Form2(name, pwd);
            //mf.Show();  //打开Mainform窗口
            //this.Hide();    //隐藏登陆窗口（是主窗口,关闭则整个应用程序也退出，只能隐藏）
        }

        /// <summary>
        /// 登陆按钮点击事件
        /// </summary>
        /// <param name="sender"></param>
        /// <param name="e"></param>
        private void Btn_OK_Click(object sender, EventArgs e)
        {
            //myTimer.Tick += new EventHandler(函数名); //给timer挂起事件 
           
            myTimer.Tick += new EventHandler(myTimer_Tick);
            myTimer.Enabled = true;//使timer可用
            myTimer.Interval = 2000;//设置时间间隔，以毫秒为单位
            myTimer.Start();
           
            
            
        }

        //计时器到时的回调函数
        void myTimer_Tick(object sender, EventArgs e)
        {
            textBox1.Text += DateTime.Now.ToShortDateString() + "执行\r\n";
           /* cipid = common.Functions.getMaxid("directory", "directoryid", SqlConnStr);
           
                try
                {
                    serivce.GetDirectory(cipid, 196, 169);
                    serivce.GetDirectoryAsync(cipid, 196, 169);
                    serivce.GetDirectoryCompleted += new GetDirectoryCompletedEventHandler(serivce_GetDirectoryCompleted);

                    cipid += 31;

                    MessageBox.Show(cipid.ToString());
                    textBox1.Text = DateTime.Now.ToShortDateString() + "增加了30个目录" + cipid.ToString();
                }
                catch (Exception ex)
                {
                    Console.WriteLine(ex.Message);

                }
           */
            //MessageBox.Show("11");
            //throw new NotImplementedException();
        }

        void serivce_GetDirectoryCompleted(object sender, GetDirectoryCompletedEventArgs e)
        {
            DataSet ret = e.Result;
            DataTable dt = ret.Tables[0];
            string kk = "";
            for (int i = 0; i < dt.Rows.Count; i++)
            {
                //检查数据是否重复
                if (chickEx("directoryid=" + dt.Rows[i][0].ToString(), "directory"))
                {
                    kk = "delete from directory where directoryid=" + dt.Rows[i][0].ToString();
                    common.Functions.ExecuteSql(kk, SqlConnStr);

                }
                kk = "insert directory (directoryid,bookid,siteid,dirurl) values ('";
                kk += dt.Rows[i][0].ToString() + "','";
                kk += dt.Rows[i][1].ToString() + "','";
                kk += dt.Rows[i][2].ToString() + "','";
                kk += dt.Rows[i][3].ToString() + "')";
                common.Functions.ExecuteSql(kk, SqlConnStr);
            }
            
        }
        bool chickEx(string whe, string table)
        {
            string sql = "select * from " + table + " where " + whe;
            if (common.Functions.getDatatable(sql, SqlConnStr).Rows.Count == 0)
                return false;
            else
                return true;
        }

        private void button1_Click(object sender, EventArgs e)
        {

            //停止按钮
            myTimer.Stop(); //如果要暂停计时则使用Stop()方法
            myTimer.Enabled = false;//若要停止使用timer，则使之不可用
            textBox1.Text += DateTime.Now.ToShortDateString() + "停止执行\r\n";
        }

    
    }
}
